!(function ($) {
    ace.demo = {
        functions: {},
        init: function (isBrowser) {
            var a = !!isBrowser && true;
            if ("undefined" === typeof requirejs || a) {
                for (var fn in ace.demo.functions) {
                    if (ace.demo.functions.hasOwnProperty(fn)) {
                        ace.demo.functions[fn]()
                    }
                }
            }
        }
    };

    ace.demo.functions.basics = function () {
        ace.vars.non_auto_fixed && $("body").addClass("mob-safari");
        ace.vars.transition = ace.vars.animation || !!$.support.transition
    };

    /**
     * 左侧菜单
     */
    ace.demo.functions.enableSidebar = function () {
        var $sidebar = $(".sidebar");
        $.fn.ace_sidebar && $sidebar.ace_sidebar();
        $.fn.ace_sidebar_scroll && $sidebar.ace_sidebar_scroll({include_toggle: ace.vars.safari || ace.vars.ios_safari});
        $.fn.ace_sidebar_hover && $sidebar.ace_sidebar_hover({
            sub_hover_delay: 750,
            sub_scroll_style: "no-track scroll-thin scroll-margin scroll-visible"
        })
    };

    /**
     * 滚动到顶部
     */
    ace.demo.functions.scrollTop = function() {
        !function ($) {
            var $btnUp = $(".btn-scroll-up");
            if ($btnUp.length > 0) {
                var d = !1;
                $(window).on("scroll.scroll_btn", function () {
                    var a = ace.helper.scrollTop(), b = ace.helper.winHeight(), e = document.body.scrollHeight;
                    a > parseInt(b / 4) || a > 0 && e >= b && b + a >= e - 1 ? d || ($btnUp.addClass("display"), d = !0) : d && ($btnUp.removeClass("display"), d = !1)
                }).triggerHandler("scroll.scroll_btn"), $btnUp.on(ace.click_event, function () {
                    var b = Math.min(500, Math.max(100, parseInt(ace.helper.scrollTop() / 3)));
                    return $("html,body").animate({scrollTop: 0}, b), !1
                })
            }
        }(window.jQuery)
    };

    ace.demo.functions.enableDemoAjax = function () {
        if ($.fn.ace_ajax) {

            window.Pace && (window.paceOptions = {ajax: true, document: true, eventLag: false});

            var settings = {
                close_active: true,
                close_mobile_menu: "#sidebar",
                close_dropdowns: true,
                default_url: "page/index",
                content_url: function (url) {

                    if (!url.match(/^page\//)) {
                        return false;
                    }

                    var path = document.location.pathname;
                    if (path.match(/(\/ajax\/)(index\.html)?/)) {
                        return path.replace(/(\/ajax\/)(index\.html)?/, "/ajax/content/" + url.replace(/^page\//, "") + ".html")
                    } else {
                        return path + "?" + url.replace(/\//, "=")
                    }
                }
            };

            settings.loading_overlay = "body";
            $("[data-ajax-content=true]").ace_ajax(settings);
            $(window).on("error.ace_ajax", function () {
                $("[data-ajax-content=true]").each(function () {
                    var $self = $(this);
                    if ($self.ace_ajax("working")) {
                        window.Pace && Pace.running && Pace.stop();
                        $self.ace_ajax("stopLoading", true)
                    }
                })
            })
        }
    };

    ace.demo.functions.handleScrollbars = function () {
        var b = !!$.fn.ace_scroll;
        b && $(".dropdown-content").ace_scroll({
            reset: !1,
            mouseWheelLock: !0
        }), b && !ace.vars.old_ie && ($(window).on("resize.reset_scroll", function () {
            $(".ace-scroll:not(.scroll-disabled)").not(":hidden").ace_scroll("reset")
        }), b && $(document).on("settings.ace.reset_scroll", function (b, c) {
            "sidebar_collapsed" == c && $(".ace-scroll:not(.scroll-disabled)").not(":hidden").ace_scroll("reset")
        }))
    };

    ace.demo.functions.dropdownAutoPos = function () {
        $(document).on("click.dropdown.pos", '.dropdown-toggle[data-position="auto"]', function () {
            var b = $(this).offset(), c = $(this.parentNode);
            parseInt(b.top + $(this).height()) + 50 > ace.helper.scrollTop() + ace.helper.winHeight() - c.find(".dropdown-menu").eq(0).height() ? c.addClass("dropup") : c.removeClass("dropup")
        })
    };

    ace.demo.functions.navbarHelpers = function () {
        $('.ace-nav [class*="icon-animated-"]').closest("a").one("click", function () {
            var b = $(this).find('[class*="icon-animated-"]').eq(0),
                c = b.attr("class").match(/icon-animated-([\d\w]+)/);
            b.removeClass(c[0])
        });

        $(document).on("click", ".dropdown-navbar .nav-tabs", function (evt) {
            evt.stopPropagation();
            var $tab = $(evt.target).closest("[data-toggle=tab]");
            if ($tab.length > 0) {
                $tab.tab("show");
                evt.preventDefault();
                $(window).triggerHandler("resize.navbar.dropdown")
            }
        })
    };

    ace.demo.functions.sidebarTooltips = function () {
        $(".sidebar .nav-list .badge[title],.sidebar .nav-list .badge[title]").each(function () {
            var b = $(this).attr("class").match(/tooltip\-(?:\w+)/);
            b = b ? b[0] : "tooltip-error", $(this).tooltip({
                placement: function (b, c) {
                    var d = $(c).offset();
                    return parseInt(d.left) < parseInt(document.body.scrollWidth / 2) ? "right" : "left"
                },
                container: "body",
                template: '<div class="tooltip ' + b + '"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
            })
        })
    };

    ace.demo.functions.someBrowserFix = function () {
        if (ace.vars.webkit) {
            var b = $(".ace-nav").get(0);
            b && $(window).on("resize.webkit_fix", function () {
                ace.helper.redraw(b)
            })
        }
        ace.vars.ios_safari && $(document).on("ace.settings.ios_fix", function (b, c, d) {
            "navbar_fixed" == c && ($(document).off("focus.ios_fix blur.ios_fix", "input,textarea,.wysiwyg-editor"), 1 == d && $(document).on("focus.ios_fix", "input,textarea,.wysiwyg-editor", function () {
                $(window).on("scroll.ios_fix", function () {
                    var b = $("#navbar").get(0);
                    b && ace.helper.redraw(b)
                })
            }).on("blur.ios_fix", "input,textarea,.wysiwyg-editor", function () {
                $(window).off("scroll.ios_fix")
            }))
        }).triggerHandler("ace.settings.ios_fix", ["navbar_fixed", "fixed" == $("#navbar").css("position")])
    };

    ace.demo.functions.bsCollapseToggle = function () {
        $(document).on("hide.bs.collapse show.bs.collapse", function (b) {
            var c = b.target.getAttribute("id"), d = $('a[href*="#' + c + '"]');
            0 == d.length && (d = $('a[data-target*="#' + c + '"]')), 0 != d.length && d.find(ace.vars[".icon"]).each(function () {
                var c, d = $(this), e = null, f = null;
                return (e = d.attr("data-icon-show")) ? f = d.attr("data-icon-hide") : (c = d.attr("class").match(/fa\-(.*)\-(up|down)/)) && (e = "fa-" + c[1] + "-down", f = "fa-" + c[1] + "-up"), e ? ("show" == b.type ? d.removeClass(e).addClass(f) : d.removeClass(f).addClass(e), !1) : void 0
            })
        })
    };

    ace.demo.functions.smallDeviceDropdowns = function () {
        function b() {
            var b = $(this).find("> .dropdown-menu");
            if ("fixed" == b.css("position")) {
                var d = parseInt($(window).width()), e = d > 320 ? 60 : d > 240 ? 40 : 30, f = parseInt(d) - e,
                    g = parseInt($(window).height()) - 30, h = parseInt(Math.min(f, 320));
                b.css("width", h);
                var i = !1, j = 0, k = b.find(".tab-pane.active .dropdown-content.ace-scroll");
                0 == k.length ? k = b.find(".dropdown-content.ace-scroll") : i = !0;
                var l = k.closest(".dropdown-menu"), m = b[0].scrollHeight;
                if (1 == k.length) {
                    var n = k.find(".scroll-content")[0];
                    n && (m = n.scrollHeight), j += l.find(".dropdown-header").outerHeight(), j += l.find(".dropdown-footer").outerHeight();
                    var o = l.closest(".tab-content");
                    0 != o.length && (j += o.siblings(".nav-tabs").eq(0).height())
                }
                var p = parseInt(Math.min(g, 480, m + j)), q = parseInt(Math.abs((f + e - h) / 2)),
                    r = parseInt(Math.abs((g + 30 - p) / 2)), s = parseInt(b.css("z-index")) || 0;
                if (b.css({
                        height: p,
                        left: q,
                        right: "auto",
                        top: r - (i ? 3 : 1)
                    }), 1 == k.length && (ace.vars.touch ? k.ace_scroll("disable").css("max-height", p - j).addClass("overflow-scroll") : k.ace_scroll("update", {size: p - j}).ace_scroll("enable").ace_scroll("reset")), b.css("height", p + (i ? 7 : 2)), b.hasClass("user-menu")) {
                    b.css("height", "");
                    var t = $(this).find(".user-info");
                    1 == t.length && "fixed" == t.css("position") ? t.css({
                        left: q,
                        right: "auto",
                        top: r,
                        width: h - 2,
                        "max-width": h - 2,
                        "z-index": s + 1
                    }) : t.css({left: "", right: "", top: "", width: "", "max-width": "", "z-index": ""})
                }
                $(this).closest(".navbar.navbar-fixed-top").css("z-index", s)
            } else 0 != b.length && c.call(this, b)
        }

        function c(b) {
            if (b = b || $(this).find("> .dropdown-menu"), b.length > 0 && (b.css({
                    width: "",
                    height: "",
                    left: "",
                    right: "",
                    top: ""
                }).find(".dropdown-content").each(function () {
                    ace.vars.touch && $(this).css("max-height", "").removeClass("overflow-scroll");
                    var b = parseInt($(this).attr("data-size") || 0) || $.fn.ace_scroll.defaults.size;
                    $(this).ace_scroll("update", {size: b}).ace_scroll("enable").ace_scroll("reset")
                }), b.hasClass("user-menu"))) {
                $(this).find(".user-info").css({
                    left: "",
                    right: "",
                    top: "",
                    width: "",
                    "max-width": "",
                    "z-index": ""
                })
            }
            $(this).closest(".navbar").css("z-index", "")
        }

        ace.vars.old_ie || $(document).on("shown.bs.dropdown.navbar", ".ace-nav > li.dropdown-modal", function (c) {
            b.call(this);
            var d = this;
            $(window).on("resize.navbar.dropdown", function () {
                b.call(d)
            })
        }).on("hidden.bs.dropdown.navbar", ".ace-nav > li.dropdown-modal", function (b) {
            $(window).off("resize.navbar.dropdown"), c.call(this)
        })
    }

})(jQuery);

jQuery(function () {
    try {
        ace.demo.init()
    } catch (err) {}
});


jQuery(function($) {

    $(document).on("reload.ace.widget", ".widget-box", function () {
        var $widgetBox = $(this);
        setTimeout(function () {
            $widgetBox.trigger("reloaded.ace.widget")
        }, parseInt(1e3 * Math.random() + 1e3))
    });

    ace.vars.US_STATES = [
        "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut",
        "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",
        "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan",
        "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
        "New Jersey", "New Mexico", "New York", "North Dakota", "North Carolina", "Ohio",
        "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
        "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"
    ];
    try {
        $("#nav-search-input").bs_typeahead({
            source: ace.vars.US_STATES,
            updater: function (b) {
                $("#nav-search-input").focus()
                return b
            }
        })
    } catch (c) {}

})